//Unit marker script by Celery
if (isDedicated) exitWith {};

_vehiclemarkers = ["b_armor", "b_mech_inf", "b_motor_inf", "b_plane", "AntiAir", "x_art", "x_mortar", "b_med", "b_support", "b_service", "b_maint"];
_vehicletypes =
[
	[""],
	["B_Quadbike_F", "B_Hunter_F", "O_Quadbike_F", "O_Ifrit_F", "c_offroad"],
	["B_Hunter_RCWS_F", "B_Hunter_HMG_F", "O_Ifrit_MG_F", "O_Ifrit_GMG_F"],
	["B_MH9_F", "B_AH9_F", "O_Ka60_F", "O_Ka60_Unarmed_F"],
	[""],
	[""],
	[""],
	[""],
	[""],
	[""],
	[""]
];


_showableunits = [];
_markers = [];
_unitupdate = 0;
sleep 0.05;
while {true} do
{
	if (time > _unitupdate) then
	{
		_showableunits = [];
		{
			if (alive _x && side _x == playerSide) then
			{
				if !(vehicle _x in _showableunits) then
				{
					_showableunits set [count _showableunits, vehicle _x];
				};
				
			};
		} forEach allUnits;
		while {count _markers < count _showableunits} do
		{
			_marker = createMarkerLocal [format ["unitmarker%1", count _markers], getPos player];
			_markers set [count _markers, _marker];
		};
		_unitupdate = time + 1;
	};
	_finalindex = 0;
	{
		_finalindex = _forEachIndex;
		_unit = _x;
		_marker = _markers select _forEachIndex;
		_color = "ColorWhite";
		if ({_x in units group player} count crew _unit == 0) then
		{
			switch (side _unit) do
			{
				case west : {_color = "ColorBLUFOR"};
				case east : {_color = "ColorOPFOR"};
				case resistance : {_color = "ColorIndependent"};
			};
		};
		_type = "mil_triangle";
		_text = "";
		_dir = 0;
		if (_unit isKindOf "Man") then
		{
			if ({_unit != _x && _unit distance vehicle _x < 10} count _showableunits == 0) then {_text = name _unit;};
			if (getNumber (configFile / "CfgVehicles" / (typeOf _unit) / "attendant") == 1) then
			{
				_type = "mil_objective";
				_dir = 45;
				_marker setMarkerSizeLocal (if (_unit == vehicle player) then {[0.5, 0.5];} else {[0.4, 0.4];});
			}
			else
			{
				_marker setMarkerSizeLocal (if (_unit == vehicle player) then {[0.3, 0.6];} else {[0.25, 0.5];});
			};
		}
		else
		{
			_type = "mil_arrow2";
			{
				_array = _x;
				if ({typeOf _unit == _x} count _array > 0) then
				{
					_type = _vehiclemarkers select _forEachIndex;
				};
			} forEach _vehicletypes;
			_names = 0;
			{
				_unit = _x;
				if (!isNull _x) then
				{
					if (_names < 3) then
					{
						if (_text != "") then {_text = _text + ", ";};
						_text = _text + name _x;
					};
					_names = _names + 1;
				};
			} forEach crew _unit;
			if (_names > 3) then
			{
				_text = _text + " + " + str (_names - 3);
			};
			if ({_x in units group player} count crew _unit > 0) then
			{
				
			};
			_marker setMarkerSizeLocal [1, 1];
		};
		_marker setMarkerTypeLocal _type;
		_marker setMarkerColorLocal _color;
		_marker setMarkerTextLocal _text;
		_marker setMarkerPosLocal getPos vehicle _unit;
		if (_type in ["mil_triangle", "mil_arrow2", "b_plane"]) then {_dir = getDir vehicle _unit;};
		_marker setMarkerDirLocal _dir;
		_marker setMarkerAlphaLocal (if (_unit == vehicle player) then {1;} else {0.7;});
	} forEach _showableunits;
	{
		if (_forEachIndex > _finalindex) then
		{
			_x setMarkerAlphaLocal 0;
		};
	} forEach _markers;
	sleep 0.1;
};
{deleteMarkerLocal _x;} forEach _markers;